package com.msg.chestionar.entities;

import java.util.ArrayList;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.msg.chestionar.to.IntrebareOptiuneTO;
import com.msg.chestionar.to.IntrebareTO;

@NamedQueries( {
		@NamedQuery(name = "IntrebareE.findByID", query = "SELECT u FROM IntrebareE u WHERE u.id = :id") 
		})
@Entity
@Table(name = "T_INTREBARE")
@SequenceGenerator(name = "SEQ_INTREBARE", sequenceName = "SEQ_INTREBARE", allocationSize = 1)
public class IntrebareE {

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_INTREBARE")
	private Long id;

	@JoinColumn(name = "chestionar_id", nullable = false)
	private ChestionarE chestionarE;

	@Column
	private String text;

	@Column
	private int tip;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "intrebareE")
	@OrderBy("id")
	private List<IntrebareOptiuneE> optiuni;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ChestionarE getChestionarE() {
		return chestionarE;
	}

	public void setChestionarE(ChestionarE chestionarE) {
		this.chestionarE = chestionarE;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getTip() {
		return tip;
	}

	public void setTip(int tip) {
		this.tip = tip;
	}

	public IntrebareTO getIntrebareTO() {
		IntrebareTO intrebare = new IntrebareTO();
		intrebare.setId(this.id);
		intrebare.setText(this.text);
		intrebare.setTip(this.tip);
		
		List<IntrebareOptiuneTO> intrOptTO = new ArrayList<IntrebareOptiuneTO>();
		for (IntrebareOptiuneE i : optiuni) {
			intrOptTO.add(i.getIntrebare_OptiuneTO());
		}
		intrebare.setOptiuni(intrOptTO);
		return intrebare;
	}

	public void setOptiuni(List<IntrebareOptiuneE> optiuni) {
		this.optiuni = optiuni;
	}

	public List<IntrebareOptiuneE> getOptiuni() {
		return optiuni;
	}

	public void addOptiune(IntrebareOptiuneE optiune) {
		if (this.optiuni == null) {
			this.optiuni = new ArrayList<IntrebareOptiuneE>();
		}
		this.optiuni.add(optiune);

	}

	public void removeOptiune(IntrebareOptiuneE optiune) {
		if (this.optiuni == null) {
			return;
		}
		this.optiuni.remove(optiune);

	}

}
